package com.htt.freeimage

import android.app.ProgressDialog
import android.content.Intent
import android.media.MediaScannerConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.*
import com.htt.image.FreeImageUtil
import java.io.File

class MainActivity : AppCompatActivity() ,View.OnClickListener{
    private lateinit var tvPath: TextView

    val CHOOSE_PHOTO=0x0001


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
    }

    private fun initViews(){
        tvPath=this.findViewById(R.id.tv_path)
        tvPath.setOnClickListener(this)
        this.findViewById<View>(R.id.bt_to_bmp).setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tv_path -> {
                //选择图片
                selectImage()
            }
            R.id.bt_to_bmp ->{
                //FreeImageUtil.imageToBmp(tvPath.text.toString())
                imageToBmp(tvPath.text.toString())
            }
        }
    }

    private fun selectImage(){
        PermissionUtils.permission(
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .callback(object : PermissionUtils.SimpleCallback{
                override fun onGranted() {
                    val intent = Intent("android.intent.action.GET_CONTENT")
                    intent.type = "image/*"
                    startActivityForResult(intent, CHOOSE_PHOTO) //打开相册
                }

                override fun onDenied() {

                }
            })
            .request()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            CHOOSE_PHOTO -> {
                if(resultCode== RESULT_OK){
                    data?.data?.also {
                        val file= UriUtils.uri2File(it)
                        tvPath.text=file.absolutePath
                    }
                }
            }
        }
    }

    private fun imageToBmp(path:String){
        if(StringUtils.isTrimEmpty(path)){
            ToastUtils.showShort("请选择图片")
            return
        }
        if(!FileUtils.isFileExists(path)){
            ToastUtils.showShort("图片不存在!")
            return
        }
        val outPath=File(getExternalFilesDir("BMP"),"${System.currentTimeMillis()}.bmp").absolutePath
        val progressDialog=ProgressDialog(this)
        progressDialog.setMessage("操作中...")
        progressDialog.setCanceledOnTouchOutside(false)
        progressDialog.setCancelable(false)
        progressDialog.show()

        ThreadUtils.executeBySingle(object:ThreadUtils.SimpleTask<Boolean>(){
            override fun doInBackground(): Boolean {
                return FreeImageUtil.imageToBmp(path,outPath)
            }

            override fun onSuccess(result: Boolean?) {
                progressDialog.dismiss()
                if(result == true){
                    ToastUtils.showShort("生成图片：$outPath")
                    val file = File(outPath)
                    MediaScannerConnection.scanFile(this@MainActivity, arrayOf(file.toString()),
                        null, null)
                }else{
                    ToastUtils.showShort("操作失败")
                }
            }

            override fun onFail(t: Throwable?) {
                super.onFail(t)
                progressDialog.dismiss()
                ToastUtils.showShort(t?.message)
            }

        })
    }

}